package wcg;

import java.awt.GridBagLayout;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JSlider;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class PanelwinFormatChecker extends JPanel {

	private static final long serialVersionUID = 1L;
	private JButton jButtonDoCheck = null;
	private boolean Busy=false;
	private JSlider jSliderOption = null;
	private JPanel jPanelTop = null;
	private JPanel jPanelOption = null;
	private JLabel jLabel1 = null;
	private JLabel jLabelDisplayLevelHi = null;
	private JLabel jLabelDisplayLevelLo = null;
	private JCheckBox jCheckBoxCheckContinum = null;
	private winchk Checker = null;
	private JButton jButtonStop = null;
	//public CheckerView checkerView = null;
	private JScrollPane jScrollPane = null;
	private FileDropTable jTableFiles = null;
	/**
	 * This is the default constructor
	 */
	public PanelwinFormatChecker() {
		super();
		initialize();
		Checker = new winchk();
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 220);
		this.setMinimumSize(new Dimension(600,100));
		this.setLayout(new BorderLayout());
		this.add(getJPanelTop(), BorderLayout.NORTH);
		this.add(getJPanelOption(), BorderLayout.CENTER);
	}


	/**
	 * This method initializes jButtonDoCheck	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonDoCheck() {
		if (jButtonDoCheck == null) {
			jButtonDoCheck = new JButton();
			jButtonDoCheck.setText("Check");
			jButtonDoCheck.setBounds(new Rectangle(3, 5, 85, 30));
			jButtonDoCheck.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(!Checker.Busy){
						try {
//							String file = new String(jTextFieldFiles.getText());
//							TreeSet<String> tset = FileDropTarget.GetFiles(file);
//							String[] files = new String[tset.size()];
							//String[] files = file.split(";",100);
						 	//int max1 = files.length;
							Vector<String> files = jTableFiles.getFileNames();
						 	int max1 = files.size();
							
							String[] args = new String[max1+2];
							int i=0;
//							Iterator<String> ite = tset.iterator();
							Iterator<String> ite = files.iterator();
							while(ite.hasNext()){
								args[i++] = ite.next();
							}
							//for(;i<max1;i++){
								//args[i]=files[i];
							//}
							String op = new String("");
							int level = jSliderOption.getValue();
//							if(level>2){
//								if(JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(null, "Detail message level will take very long time.\nContinue any way?",
//										"Are you really OK",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE)){
//									return;
//								}
//							}
							op = String.format("-v%d", level);	// spacify the display level vorbose .. debug level
							args[i++] = op;
							if(jCheckBoxCheckContinum.isSelected()){
								args[i++] = "-i";	// individual only
						  	}
							//winchk ck = new winchk();
							//ck.check(args);
							Checker = new winchk();
							Checker.Stop=false;
							Checker.Argument = args;
							Thread th = new Thread(Checker);
							th.start();
							Busy=true;
							
						 }catch(Exception ex){
					    	ex.printStackTrace();
							Busy=false;
						 }
					}
				}
			});
		}
		return jButtonDoCheck;
	}

	/**
	 * This method initializes jSliderOption	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJSliderOption() {
		if (jSliderOption == null) {
			jSliderOption = new JSlider();
			jSliderOption.setBounds(new Rectangle(409, 33, 87, 32));
			jSliderOption.setMaximum(3);
			jSliderOption.setMinimum(0);
			jSliderOption.setMajorTickSpacing(1);
			jSliderOption.setMinorTickSpacing(1);
			jSliderOption.setPaintTicks(true);
			jSliderOption.setSnapToTicks(true);
			jSliderOption.setValue(1);
			jSliderOption.setOrientation(JSlider.HORIZONTAL);
			jSliderOption.addChangeListener(new javax.swing.event.ChangeListener() {
				public void stateChanged(javax.swing.event.ChangeEvent e) {
					Checker.DisplayLevel= jSliderOption.getValue();
				}
			});
		}
		return jSliderOption;
	}

	/**
	 * This method initializes jPanelTop	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelTop() {
		if (jPanelTop == null) {
			jPanelTop = new JPanel();
			jPanelTop.setLayout(new BorderLayout());
			jPanelTop.add(getJScrollPane(), BorderLayout.NORTH);  // Generated
		}
		return jPanelTop;
	}

	/**
	 * This method initializes jPanelOption	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelOption() {
		if (jPanelOption == null) {
			jLabelDisplayLevelLo = new JLabel();
			jLabelDisplayLevelLo.setBounds(new Rectangle(500, 35, 55, 16));
			jLabelDisplayLevelLo.setText("Detail");
			jLabelDisplayLevelHi = new JLabel();
			jLabelDisplayLevelHi.setBounds(new Rectangle(354, 36, 55, 16));
			jLabelDisplayLevelHi.setText("Silent");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(207, 32, 141, 26));
			jLabel1.setText("Check&message level");
			jPanelOption = new JPanel();
			jPanelOption.setLayout(null);
			//jPanelOption.setPreferredSize(new Dimension(300,100));
			jPanelOption.add(getJSliderOption(), null);
			jPanelOption.add(jLabel1, null);
			jPanelOption.add(jLabelDisplayLevelHi, null);
			jPanelOption.add(jLabelDisplayLevelLo, null);
			jPanelOption.add(getJCheckBoxCheckContinum(), null);
			jPanelOption.add(getJButtonStop(), null);
			jPanelOption.add(getJButtonDoCheck(), null);
		}
		return jPanelOption;
	}

	/**
	 * This method initializes jCheckBoxCheckContinum	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxCheckContinum() {
		if (jCheckBoxCheckContinum == null) {
			jCheckBoxCheckContinum = new JCheckBox();
			jCheckBoxCheckContinum.setBounds(new Rectangle(204, 6, 187, 27));
			jCheckBoxCheckContinum.setText("Don't check time continuum");
			jCheckBoxCheckContinum.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					Checker.InterCheck = !jCheckBoxCheckContinum.isSelected();
				}
			});
		}
		return jCheckBoxCheckContinum;
	}

	/**
	 * This method initializes jButtonStop	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStop() {
		if (jButtonStop == null) {
			jButtonStop = new JButton();
			jButtonStop.setBounds(new Rectangle(93, 5, 85, 30));
			jButtonStop.setText("Abort");
			jButtonStop.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Checker.Stop=true;
					Busy=false;
				}
			});
		}
		return jButtonStop;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTableFiles());  // Generated
			jScrollPane.setPreferredSize(new Dimension(300,150));
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTableFiles	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private FileDropTable getJTableFiles() {
		if (jTableFiles == null) {
			jTableFiles = new FileDropTable();
		}
		return jTableFiles;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
